package com.lhn.netty.nio;

import java.nio.ByteBuffer;

/**
 * @author lhn
 * @date 2025/05/26 20:02
 * @since 1.0.0
 **/
public class BufferUnderflowExceptionDemo {

    public static void main(String[] args) {
        ByteBuffer buffer = ByteBuffer.allocate(4);
        buffer.putInt(100);      // 写入 4 个字节（int 占 4 个字节）

        buffer.flip();           // 读写切换：position = 0，limit = 4

        buffer.getShort();       // 尝试读取 2 个字节 -> OK，position = 2
        buffer.getShort();       // 再尝试读取 2 个字节 -> OK，position = 4
        buffer.getShort();       // ❌ 此时 buffer 剩余 0 字节 -> 报 BufferUnderflowException

    }

}